package net.simflow.swf.analyzer.gui;

import java.util.ArrayList;

import net.simflow.swf.analyzer.opt.FontDataClone;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

public class ChooseDialog extends Dialog {

	protected Shell shell;
	protected ArrayList<String> content;
	protected String result;
	private Label lblTitle;
	private Composite cmpContent;

	private Point size;
	private Table tblContent;

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public ChooseDialog(Shell parent, String title, ArrayList<String> content,
			Point size) {
		super(parent, SWT.CLOSE | SWT.MIN | SWT.MAX | SWT.TITLE);
		setText(title);
		this.size = size;
		this.content = content;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public String open() {
		createContents();
		if (this.content != null) {
			fillContents(content);
		}
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {

		shell = new Shell(getParent(), SWT.CLOSE | SWT.MIN | SWT.MAX
				| SWT.TITLE | SWT.APPLICATION_MODAL);
		shell.setSize(size);
		shell.setText(getText());

		shell.addListener(SWT.Traverse, new Listener() {
			public void handleEvent(Event e) {
				if (e.detail == SWT.TRAVERSE_ESCAPE) {
					e.doit = false;
				}
			}
		});

		int parlocx = shell.getParent().getLocation().x;
		int parlocy = shell.getParent().getLocation().y;
		int parw = shell.getParent().getSize().x;
		int parh = shell.getParent().getSize().y;
		int thisw = shell.getSize().x;
		int thish = shell.getSize().y;
		int locx = parw / 2 + parlocx - thisw / 2;
		int locy = parh / 2 + parlocy - thish / 2;
		shell.setLocation(locx, locy);

		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.marginWidth = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		shell.setLayout(gridLayout);

		Composite cmpMain = new Composite(shell, SWT.NONE);
		GridLayout gl_cmpMain = new GridLayout(1, false);
		gl_cmpMain.horizontalSpacing = 0;
		gl_cmpMain.verticalSpacing = 0;
		gl_cmpMain.marginWidth = 0;
		gl_cmpMain.marginHeight = 0;
		cmpMain.setLayout(gl_cmpMain);
		cmpMain.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		Composite cmpTitle = new Composite(cmpMain, SWT.NONE);
		cmpTitle.setLayout(new GridLayout(1, false));
		GridData gd_cmpTitle = new GridData(SWT.FILL, SWT.FILL, true, false, 1,
				1);
		gd_cmpTitle.heightHint = 40;
		cmpTitle.setLayoutData(gd_cmpTitle);

		Composite cmpTitleContent = new Composite(cmpTitle, SWT.NONE);
		cmpTitleContent.setLayout(new GridLayout(1, false));
		cmpTitleContent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true, 1, 1));

		lblTitle = new Label(cmpTitleContent, SWT.NONE);
		lblTitle.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
				1));
		FontData fontData = FontDataClone.clone(Display.getCurrent()
				.getSystemFont().getFontData()[0]);
		fontData.setHeight(fontData.getHeight() + 1);
		fontData.setStyle(SWT.BOLD);
		Font font = new Font(Display.getCurrent(), fontData);
		lblTitle.setFont(font);
		lblTitle.setText(getText());

		Label lineRight = new Label(cmpMain, SWT.SEPARATOR | SWT.HORIZONTAL);
		lineRight.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		cmpContent = new Composite(cmpMain, SWT.NONE);
		cmpContent.setLayout(new GridLayout(1, false));
		cmpContent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true,
				1, 1));

		tblContent = new Table(cmpContent, SWT.BORDER | SWT.FULL_SELECTION);
		tblContent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true,
				1, 1));
		tblContent.setLinesVisible(true);

		Label line = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		line.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Composite cmpBottom = new Composite(shell, SWT.NONE);
		GridLayout gl_cmpBottom = new GridLayout(2, false);
		gl_cmpBottom.marginWidth = 0;
		gl_cmpBottom.marginHeight = 10;
		cmpBottom.setLayout(gl_cmpBottom);
		cmpBottom.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,
				1, 1));

		Composite cmpButtonBlank = new Composite(cmpBottom, SWT.NONE);
		GridData gd_cmpButtonBlank = new GridData(SWT.FILL, SWT.FILL, true,
				false, 1, 1);
		gd_cmpButtonBlank.heightHint = 15;
		cmpButtonBlank.setLayoutData(gd_cmpButtonBlank);
		cmpButtonBlank.setBounds(0, 0, 64, 64);

		Composite cmpButton = new Composite(cmpBottom, SWT.NONE);
		cmpButton.setLayout(new FillLayout(SWT.HORIZONTAL));
		GridData gd_cmpButton = new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1);
		gd_cmpButton.widthHint = 263;
		cmpButton.setLayoutData(gd_cmpButton);

		Button btnOk = new Button(cmpButton, SWT.NONE);
		btnOk.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				save();
				exit();
			}
		});
		btnOk.setText("OK");

		Button btnCancel = new Button(cmpButton, SWT.NONE);
		btnCancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				exit();
			}
		});
		btnCancel.setText("Cancel");
	}

	private void fillContents(ArrayList<String> content) {
		for (String p : content) {
			TableItem item = new TableItem(tblContent, SWT.NONE);
			item.setText(p.toString());
		}
		tblContent.select(0);
		tblContent.pack();
	}

	private void save() {
		if (content == null) {
			result = null;
			return;
		}

		int s = tblContent.getSelectionIndex();
		if (s >= 0 && s < tblContent.getItemCount()) {
			result = this.content.get(s);
		} else {
			result = null;
		}
	}

	private void exit() {
		shell.dispose();
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
